package com.controller;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.dto.registration.RegistrationDTO;
import com.model.dao.GuestTable;
import com.model.dao.GuestTableDAO;
import com.model.dao.UserTable;
import com.model.dao.UserTableDAO;

@ManagedBean(name="registrationController")
@SessionScoped
public class RegistrationController implements Serializable{

	private static final long serialVersionUID = -1438592151166246832L;

	/*
	 * based on registration type create entry in user table or guest table*/
	public Integer persist(RegistrationDTO dto) throws Exception {
		Integer id;
		if(dto.getRegistrationType().equalsIgnoreCase("N")){
			id=createUserEntry(dto);
		}else{
			id=createGuestEntry(dto);
		}
		return id;
	}
	private Integer createUserEntry(RegistrationDTO dto) throws Exception{
		UserTableDAO dao = new UserTableDAO();
		UserTable transientInstance = new UserTable();
		transientInstance.setArea(dto.getArea());
		transientInstance.setCity(dto.getCity());
		transientInstance.setEmail(dto.getEmail());
		transientInstance.setHouseno(dto.getHouseNo());
		transientInstance.setLandmark(dto.getLandmark());
		transientInstance.setMobileno(dto.getMobileNo());
		transientInstance.setName(dto.getName());
		transientInstance.setPassword(dto.getPassword());
		transientInstance.setStreet(dto.getStreet());
		return dao.persist(transientInstance);
	}
	private Integer createGuestEntry(RegistrationDTO dto) throws Exception{
		GuestTableDAO dao = new GuestTableDAO();
		GuestTable transientInstance = new GuestTable();
		transientInstance.setArea(dto.getArea());
		transientInstance.setCity(dto.getCity());
		transientInstance.setEmail(dto.getEmail());
		transientInstance.setHouseno(dto.getHouseNo());
		transientInstance.setLandmark(dto.getLandmark());
		transientInstance.setMobileno(dto.getMobileNo());
		transientInstance.setName(dto.getName());
		transientInstance.setStreet(dto.getStreet());
		return dao.persist(transientInstance);
	}
	public boolean validateDuplicateUser(String uid) throws Exception{
		UserTableDAO dao = new UserTableDAO();
		UserTable table = dao.findByUserName(uid);
		boolean userExistFlag;
		if(null!=table){
			userExistFlag= true;
		}else {
			userExistFlag= false;
		}
		return userExistFlag;
	}
}
